package com.ericom.webapp.action;

import java.util.List;

import com.ericom.model.Brand;
import com.ericom.service.GenericManager;

public class BrandAction extends BaseAction {
	private GenericManager<Brand, Long> brandManager;
	private List brands;
	private Brand brand;
	private Long id;

	public Brand getBrand() {
		return brand;
	}

	public void setBrand(Brand brand) {
		this.brand = brand;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public void setBrandManager(GenericManager<Brand, Long> brandManager) {
		this.brandManager = brandManager;
	}

	public String list() {
		brands = brandManager.getAll();
		return SUCCESS;
	}

	public List getBrands() {
		return brands;
	}

	public String delete() {
		brandManager.remove(brand.getId());
		this.saveMessage(getText("brand.deleted"));
		return this.SUCCESS;
	}

	public String edit() {
		if (id != null) {
			brand = brandManager.get(id);
		} else {
			brand = new Brand();
		}
		return this.SUCCESS;
	}

	public String save() throws Exception {
		if (this.cancel != null) {
			return this.CANCEL;
		}
		if (this.delete != null) {
			return this.delete();
		}

		boolean isNew = (brand.getId() == null);
		brand = brandManager.save(brand);

		String key = (isNew) ? "brand.added" : "brand.updated";
		this.saveMessage(getText(key));

		if (!isNew) {
			return this.INPUT;
		} else {
			return this.SUCCESS;
		}
	}
}
